package edu.gdkm.sql.dao;

import edu.gdkm.sql.pojo.Order;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface OrderMapper {

    // 查询全部订单
    @Select("SELECT * FROM `order`")
    @Results(id="orderMap",value = {
            @Result(property = "id", column = "id"),
            @Result(property = "userinfoid", column = "userinfoid"),
            @Result(property = "orderdate", column = "orderdate"),
            @Result(property = "orderdetails", column = "id",
                    many = @Many(select = "edu.gdkm.sql.dao.OrderdetailMapper.findByOrderid"))
    })

    List<Order> findAll();

    // 根据id查询订单
    @Select("SELECT * FROM `order` WHERE id = #{id}")
    @ResultMap("orderMap")
    Order findById(int id);

    // 根据用户id查询订单
    @Select("SELECT * FROM order WHERE userinfoid = #{userinfoid}")
    @ResultMap("orderMap")
    List<Order> findByUserinfoid();

    // 插入订单
    @Insert("INSERT INTO order(userinfoid, orderdate) VALUES(#{userinfoid}, #{orderdate})")

    int insertOrder(Order order);

    // 更新订单
    @Update("UPDATE order SET userinfoid=#{userinfoid}, orderdate=#{orderdate} WHERE id=#{id}")
    @ResultMap("orderMap")
    int updateOrderById(Order order);

    // 删除订单
    @Delete("DELETE FROM order WHERE id=#{id}")
    @ResultMap("orderMap")
    int deleteOrderById(int id);
}